package com.youyou.fusheng2.View.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;
import com.youyou.fusheng2.Moudle.showText;
import com.youyou.fusheng2.R;

import java.util.List;

/**
 * @name fusheng2
 * @class name：com.youyou.fusheng2.View.Adapter
 * @anthor 浮生 QQ:2567294605
 * @time 2020/8/24 00:44
 * @to
 */
public class FenleiTopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<String> leixingList;//类型的集合
    private List<String> diquList; //地区的集合
    private List<String> yearsList; //年份的集合
    private showText     showText;
    private String       s1 = "0";
    private String       s2 = "0";
    private String       s3 = "0";


    public FenleiTopAdapter(List<String> leixingList, List<String> diquList, List<String> yearsList, com.youyou.fusheng2.Moudle.showText showText) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/24  01:23
         *  @简介: 因为没有上拉刷新 所以就直接用全参构造
         */
        this.leixingList = leixingList;
        this.diquList = diquList;
        this.yearsList = yearsList;
        this.showText = showText;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.fenlei_tabs, parent, false);
        return new FenLeiTabs(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/24  01:31
         *  @简介: 根据position设置每个TabLayout的数据
         */
        switch (position) {
            case 0:
                setTabs(holder, leixingList);
                setOnclik(holder, position);
                break;
            case 1:
                setTabs(holder, diquList);
                setOnclik(holder, position);
                break;
            case 2:
                setTabs(holder, yearsList);
                setOnclik(holder, position);
                break;
        }
    }

    private void setTabs(RecyclerView.ViewHolder holder, List<String> list) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/24  01:30
         *  @简介: 设置TabLayout的子item的数据
         */
        ((FenLeiTabs) holder).fenlei_tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (int i = 0; i < list.size(); i++) {
            ((FenLeiTabs) holder).fenlei_tabs.addTab(((FenLeiTabs) holder).fenlei_tabs.newTab().setText(list.get(i)));
        }
    }

    private void setOnclik(RecyclerView.ViewHolder holder, int position) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/24  03:04
         *  @简介: 设置点击事件 数据会掉到activity
         */

        ((FenLeiTabs) holder).fenlei_tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (position == 0) {
                    s1 = ((FenLeiTabs) holder).fenlei_tabs.getTabAt(tab.getPosition()).getText().toString();
                    showText.showText(s1, s2, s3);
                } else if (position == 1) {
                    s2 = ((FenLeiTabs) holder).fenlei_tabs.getTabAt(tab.getPosition()).getText().toString();
                    showText.showText(s1, s2, s3);
                } else if (position == 2) {
                    s3 = ((FenLeiTabs) holder).fenlei_tabs.getTabAt(tab.getPosition()).getText().toString();
                    showText.showText(s1, s2, s3);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    @Override
    public int getItemCount() {
        return leixingList == null ? 0 : 3;
    }

    private static class FenLeiTabs extends RecyclerView.ViewHolder {

        public TabLayout fenlei_tabs;

        public FenLeiTabs(View inflate) {
            super(inflate);
            fenlei_tabs = inflate.findViewById(R.id.fenlei_tabs);
        }
    }
}
